package com.qf.openapi.zuul.utils;

import com.qf.openapi.zuul.feign.CacheFeignClient;
import com.qf.qfsy.constants.CacheConstants;
import com.qf.qfsy.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class LoadRouteDataApplicationRunner implements ApplicationRunner {

    @Autowired
    CacheFeignClient cacheFeignClient;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 从缓存中加载路由信息到本地

        // 查询所有路由新的key
        List<String> keys = (List<String>) cacheFeignClient.keys(CacheConstants.OPENAPI_MAPPING_PREFIX+"*").getData();

        // 循环查询所有路由信息
        for (String key : keys) {
            HashMap data = (HashMap) cacheFeignClient.hmget(key).getData();
            ZuulLocalCache.routingCache.put(key,data);
        }
        log.info("路由缓存加载完毕，缓存内容为{}",ZuulLocalCache.routingCache);
    }
}
